from json import JSONDecodeError

import requests
import json
import translate
import time
import download_pdf

headers = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
    "Connection": "keep-alive",
    "Content-Length": "67",
    "Content-Type": "application/json",
    "Cookie": "WLSESSION=186802828.20480.0000; fp=3cedca9db79827b20def4099bb80b6a2; s_fid=6E7DC0898FF8E7F5-1BF28C267B766DC4; s_cc=true; s_vi=[CS]v1|2EE4900905158000-4006EBA9C9E66188[CE]; cookieconsent_status=dismiss; JSESSIONID=TmFaLDzyjm0KgwFwpUvLjLvnWrkTnjXzUddlX5iwZ3q00vrtQFzN!1784859624; ipList=219.230.70.141; AMCVS_8E929CC25A1FB2B30A495C97%40AdobeOrg=1; AMCV_8E929CC25A1FB2B30A495C97%40AdobeOrg=1687686476%7CMCMID%7C10335982104408621797321274818794637377%7CvVersion%7C3.0.0%7CMCAAMLH-1574076065%7C11%7CMCAAMB-1574076065%7Cj8Odv6LonN4r3an7LhD3WZrU1bUpAkFkkiY1ncBR96t2PTI%7CMCOPTOUT-1573478464s%7CNONE%7CMCSYNCSOP%7C411-18219; ipCheck=2001:19f0:7001:5c84:5400:2ff:fe67:beee; TS01109a32=012f3506233d26bc0a29f08129bb952f71d6d6744da784f5aefca212384e0884f7f65fa83e253a14bb3a1b729e2d4a581c961ff6d89cd817cdb3496115314dc4b9cd41e638a674d4bc0ee6770013c1f2b5271c48346c3affd7a6a45280e7024b827891c86e0419dc8f6b7a97176718c3aca96097fb2251ca87aa66706f4dfd69cb56a848b7400db9fbb5bd15ffcf69d084c51ec50b5e78f35f7248644c26ba58a3a1416bc3; TS01cbcee7=012f350623737a01f4cfe247fb6e0678158567c7877f78115359faae6262b599b7c473743862210901ed18d69766094e8c52f14bd9abaae2a3b161f36602732e7d7b6ea5aeebec98aad27545dc0d9ccd15c471f6f22ee0adef60b2ed2c89ddd78189360f7abda47c673a5df0a9258099230ffbd66ec6f776a0871f9af73c733e122819c600; TS01dcb973_26=014082121d0d0c387c786a684920999d596e6e50e95863e37e66174e327006cf8a42f8ea6a36ae873d694568d01baad4886c84149991e54044f6cfe6cbcb1ec621f59a1a5d; TS01dcb973=012f3506230c9cfe6d00561806257d66f981f147ac0348155008a7ce4f04062cf6cc71d5b6547918fb553676600d7ee6352981448df682a9c99ba173edf9e9784b777515133ae276b548046899aaa9291003d94d0820b715363260a39474150bfae68dd2fc8c14ea86cae3a9b632a49e58e8829017e8b2972e25006565fd3541c6c634ff839e776ebf2f60f426edf26d290f112d3a94cfa76d95a78bcbcb3e8a81c0e96034066f37de5fb8fb3cc0f5ddeb6e98a768c2ba5b052cfd0338ef4d599f2c8d025a47f32684b78f16a367ebc526facee32615413521cb900b9ed9140c9310bd8c3e0fa4a930f480ad349d66f921ac715e90f379b0451fbc809d80c5a278d798ed4fdcb47b1c47759563367511f2a43611bf; utag_main=v_id:016e59a56adf007311b782e0197803072003e06a00b58$_sn:6$_ss:0$_st:1573485533070$vapi_domain:ieee.org$ses_id:1573483726459%3Bexp-session$_pn:1%3Bexp-session; s_sq=ieeexplore.prod%3D%2526c.%2526a.%2526activitymap.%2526page%253Dhttps%25253A%25252F%25252Fieeexplore.ieee.org%25252Fxpl%25252Fissues%25253Fpunumber%25253D10209%252526isnumber%25253D8805285%2526link%253DIssue%2525201%2526region%253DLayoutWrapper%2526.activitymap%2526.a%2526.c%2526pid%253Dhttps%25253A%25252F%25252Fieeexplore.ieee.org%25252Fxpl%25252Fissues%25253Fpunumber%25253D10209%252526isnumber%25253D8805285%2526oid%253Dhttps%25253A%25252F%25252Fieeexplore.ieee.org%25252Fxpl%25252Ftocresult.jsp%25253Fisnumber%25253D8638864%252526punumber%25253D10209%2526ot%253DA",
    "DNT": "1",
    "Host": "ieeexplore.ieee.org",
    "Origin": "https://ieeexplore.ieee.org",
    "Referer": "https://ieeexplore.ieee.org/xpl/tocresult.jsp?isnumber=8638864&punumber=10209&sortType=vol-only-seq&rowsPerPage=10",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-origin",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36"
}


def get_publication(page):
    publication_number = []
    try:
        r = requests.post('https://ieeexplore.ieee.org/rest/publication',
                          json={"rowsPerPage": "100", "contentType": "periodicals", "pageNumber": page,
                                "tabId": "title"},
                          headers=headers)
        result = r.json()
        if "records" in result:
            for publication in result['records']:
                if str(publication["title"]).find("Transport") != -1 or str(publication["title"]).find("Vehicle") != -1:
                    publication_number.append(publication["publicationNumber"])
                    print(publication)
        return publication_number
    except BaseException:
        print("exception: publication")
        return []


def get_issueNumber(publication_number):
    issue_dict = {}
    try:
        r = requests.get('https://ieeexplore.ieee.org/rest/publication/{}/regular-issues'.format(publication_number),
                         headers=headers)
        result = r.json()
        for item in result['issuelist']:
            for year in item["years"]:
                # print(year["year"])
                number = []
                for issue in year["issues"]:
                    # print(issue)
                    number.append(issue["issueNumber"])
                issue_dict[year["year"]] = number
        return issue_dict
    except BaseException:
        print("exception: publication_number - {}".format(publication_number))
        return []


def catch_title(punumber, isnumber):
    url = 'https://ieeexplore.ieee.org/rest/search/pub/{}/issue/{}/toc'.format(punumber, isnumber)
    # print(url)
    try:
        r = requests.post(url,
                          json={"sortType": "vol-only-seq",
                                "rowsPerPage": "100",
                                "pageNumber": 1},
                          headers=headers)
        result = r.json()
        if "records" in result:
            for article in result['records']:
                ch_title = translate.baidu_translate(article['articleTitle'])
                if str(ch_title).find("异常") != -1 or str(ch_title).find("视频") != -1 or str(ch_title).find("交通") != -1:
                    print("{},{}".format(article['articleTitle'], ch_title))
                    pdf_link = "https://ieeexplore.ieee.org/{}".format(article['pdfLink'])
                    print(pdf_link)

        else:
            print("{},{} None".format(punumber, isnumber))
            print(result)
    except BaseException:
        print("exception: publication_number - {} , issue_number - {} ".format(punumber, isnumber))


if __name__ == '__main__':
    publication_numbers = []
    for i in range(1, 5):
        time.sleep(3)
        publication_numbers.extend(get_publication(i))
    print(publication_numbers)
    for punumber in publication_numbers:
        print("\n期刊number:   {}\n".format(punumber))
        time.sleep(3)
        issue_dict = get_issueNumber(punumber)
        for key in issue_dict.keys():
            if int(key) > 2019:
                print("{}-{} 的期刊".format(punumber, key))
                for i in issue_dict[key]:
                    time.sleep(5)
                    catch_title(punumber=punumber, isnumber=str(i))
    print("all search end")
